<?php
namespace Common\Util;

class WeixinApi{

    public static function getWxContent($url){
        $r_data = array();
        $content = self::getHtmlContent($url,'utf-8');
        $title_begin 		= '<title>';
        $title_end			= '<\/title>';
        if (@preg_match("/{$title_begin}(.*?){$title_end}/is", $content, $rs)) $rs = $rs[1];
        else $rs = '';
        $r_data['title'] = $rs;

        $con_begin 		= '<div class="rich_media_content " id="js_content">';
        $con_end			= '<\/div>';
        if (@preg_match("/{$con_begin}(.*?){$con_end}/is", $content, $rs)) $rs = $rs[1];
        else $rs = '';
        $c_con = $rs;

        $c_con = preg_replace("/<img([^>]*)\s*data-src=('|\")([^'\"]+)('|\")(.*?)>/is","<img src='$3'>",$c_con);
        //下载所有图片
        if (@preg_match_all("/<img([^>]*)\s*src=('|\")([^'\"]+)('|\")/is", $c_con, $c_litpicall1)) $c_litpicall1 = $c_litpicall1[3];
        else $c_litpicall1 = '';

        if(is_array($c_litpicall1) && count($c_litpicall1))
        {
            foreach ($c_litpicall1 as $val2)
            {
                $c_litpicone1 = self::downimgall($val2);
                $c_con = str_replace($val2, $c_litpicone1, $c_con);
            }
        }
        $r_data['content'] = $c_con;
        return $r_data;
    }

    //下载全部图片
    public static function downimgall($url)
    {
        $data = @cyz_curl_get_contents($url); // 读文件内容
        //图片保存路径
        $root1 = date("Ymd",time());
        $file_path = SITE_PATH.'/Data/allimg/'.$root1.'/';
        //判断目录是否存在
        if(self::check_exist($file_path) == false)
        {
            self::createDir($file_path);
        }
        $randnum = time().rand(1, 999);
        $filename = $file_path.$randnum.'.png';
        $fp = @fopen($filename,"w"); //以写方式打开文件
        @fwrite($fp,$data);
        fclose($fp);
        //添加水印

        return get_cover_img('/Data/allimg/'.$root1.'/'.$randnum.'.png');
    }

    //检查目录或文件是否存在
    public static function check_exist($filename)
    {
        if(file_exists($filename))
        {
            return true;
        }
        else	return false;
    }
    //创建多级目录
    public static function createDir($aimUrl)		//可同时创建多级目录
    {
        $aimUrl = str_replace('\\', '/', $aimUrl);
        $aimDir = '';
        $arr = explode('/', $aimUrl);
        foreach ($arr as $str)
        {
            $aimDir .= $str . '/';
            if (!file_exists($aimDir))
            {
                mkdir($aimDir);
            }
        }
    }

    /**
     * 获取网页内容
     * @param $url
     * @param string $char
     * @return mixed|string
     */
    public static function getHtmlContent($url,$char='utf-8'){
        $c = @cyz_curl_get_contents($url);
        if ($c) {
            $c = stripslashes($c);
            if ($char) {
                $tmp_charset = $char;
            } else {
                preg_match("/<meta.+?charset=([-\w]+)/i", $c, $rs);
                $tmp_charset = $rs[1];
            }
            if (!empty($tmp_charset)) {
                $tmp_charset = strtolower($tmp_charset);
                if ($tmp_charset == 'gb2312')$tmp_charset = 'gbk';
                if (strtolower('utf-8') != $tmp_charset) {
                    $c = 'utf-8' == 'utf-8' ? iconv($tmp_charset, 'utf-8', $c) : iconv($tmp_charset, 'gbk//IGNORE', $c);
                }
            }
            $c = preg_replace("/[\r\n]+/", '', $c);
        }
        return $c;
    }
}